<div>
    <!--部门查询页面-->
    <div class="ui top fluid attach container segment">
        <div class="ui blue top attached label">用户邮箱设置</div>
        <div class="ui grid">
            <div class="row">
                <div class="four wide column">
                    <div class="ui small input">
                        <span class="all-condition-title">当前邮箱：</span>
                        <input type="text" id="currentEmail" readonly>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="four wide column">
                    <div class="ui small input">
                        <span class="all-condition-title">新的邮箱：</span>
                        <input type="text" id="newEmail">
                    </div>
                </div>
                <div class="five wide column">
                    <div class="ui small input" style="height: 35px;">
                        <span class="all-condition-title">验证码：</span>
                        <input type="text" id="verifyCode">
                        <a href="javascript:;"><img src="" id="confirmCodeImage"></a>
                    </div>
                </div>
                <div class="four wide column">
                    <button class="ui twitter small button all-button-between-margin" id="user-information-email-code">
                        验 证 码
                    </button>
                    <button class="ui twitter small button all-button-between-margin" id="user-information-email-save">
                        确 认 保 存
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="ui top fluid attach container segment">
        <div class="ui blue top attached label">用户密码修改</div>
        <div class="ui grid">
            <div class="row">
                <div class="four wide column">
                    <div class="ui small input">
                        <span class="all-condition-title">旧的密码：</span>
                        <input type="password" id="oldPassword">
                    </div>
                </div>
                <div class="four wide column">
                    <div class="ui small input">
                        <span class="all-condition-title">新的密码：</span>
                        <input type="password" id="newPassword">
                    </div>
                </div>
                <div class="four wide column">
                    <div class="ui small input">
                        <span class="all-condition-title">确认密码：</span>
                        <input type="password" id="verifyPassword">
                    </div>
                </div>
                <div class="four wide column">
                    <button class="ui twitter small button all-button-between-margin"
                            id="user-information-password-save"> 确 认 保 存
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>

    $.ajax({
        url: "getCurrentUserEmail",
        type: 'post',
        // data: "",
        // async: false,
        beforeSend: (request) => {
            if (constant.csrfToken && constant.csrfHeader) {
                request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
            }
        },
        success: (data) => {
            if (data != "") {
                $("#currentEmail").val(data);
            } else {
                $("#currentEmail").val("暂未设置邮箱！");
                showErrorPromptBox("当前邮箱加载失败！", 2);
            }
        },
        error: () => {
            showErrorPromptBox("系统繁忙，请稍后访问！", 2);
        }
    });

    $("#user-information-email-code").click(() => {
        let newEmail = $("#newEmail").val().trim();
        if ("" === newEmail) {
            showErrorPromptBox("请输入新的邮箱！", 2);
            return false;
        }
        let reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
        if (!reg.test(newEmail)) {
            showErrorPromptBox("请输入格式正确的邮箱！", 2);
            return false;
        }

        let queryParams = {
            newEmail: newEmail
        }

        $.ajax({
            url: "validate/getEmailCode",
            type: 'post',
            data: queryParams,
            // async: false,
            beforeSend: (request) => {
                if (constant.csrfToken && constant.csrfHeader) {
                    request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                }
            },
            success: (data) => {
                if (data == "SUCCESS") {
                    showSuccessPromptBox("验证码发送成功！", 2);
                } else if (data == "ERROR") {
                    showErrorPromptBox("验证码发送失败！", 2);
                }
            },
            error: () => {
                showErrorPromptBox("系统繁忙，请稍后访问！", 2);
            }
        });
    })

    $("#user-information-email-save").click(() => {
        let newEmail = $("#newEmail").val().trim();
        if ("" === newEmail) {
            showErrorPromptBox("请输入新的邮箱！", 2);
            return false;
        }
        let reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
        if (!reg.test(newEmail)) {
            showErrorPromptBox("请输入格式正确的邮箱！", 2);
            return false;
        }
        let confirmCode = $("#verifyCode").val().trim();
        if ("" === confirmCode) {
            showErrorPromptBox("请输入验证码！", 2);
            return false;
        }
        let status = verifyConfirmCode(confirmCode);
        if ("ERROR" === status) {
            showErrorPromptBox("验证码错误，请重新输入", 2);
            $("#confirmCode").val("");
            getCode();
            return false;
        }

        let queryParams = {
            newEmail: newEmail
        }

        $.ajax({
            url: "updateUserEmail",
            type: 'post',
            data: queryParams,
            // async: false,
            beforeSend: (request) => {
                if (constant.csrfToken && constant.csrfHeader) {
                    request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                }
            },
            success: (data) => {
                if (data == "SUCCESS") {
                    showSuccessPromptBox("邮箱绑定成功！", 2);
                    $("#currentEmail").val(newEmail);
                } else {
                    showErrorPromptBox("邮箱绑定失败！", 2);
                }
            },
            error: () => {
                showErrorPromptBox("系统繁忙，请稍后访问！", 2);
            }
        });
        $("#newEmail").val("");
        $("#verifyCode").val("");
    })

    let verifyConfirmCode = (code) => {
        let verifyStatus = "ERROR";
        let queryParam = {
            code: code
        }
        $.ajax({
            url: "validate/verifyCode",
            type: 'get',
            data: queryParam,
            async: false,
            beforeSend: (request) => {
                if (constant.csrfToken && constant.csrfHeader) {
                    request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                }
            },
            success: (data) => {
                verifyStatus = data;
            },
            error: () => {
                showErrorPromptBox("系统繁忙，请稍后访问！", 2);
            }
        });
        return verifyStatus;
    }

    $("#user-information-password-save").click(() => {
        let oldPassword = $("#oldPassword").val().trim();
        if ("" === oldPassword) {
            showErrorPromptBox("请输入旧密码！", 2);
            return false;
        }

        let newPassword = $("#newPassword").val().trim();
        if ("" === newPassword) {
            showErrorPromptBox("请输入新密码！", 2);
            return false;
        }

        let verifyPassword = $("#verifyPassword").val().trim();
        if ("" == verifyPassword) {
            showErrorPromptBox("请二次输入密码！", 2);
            return false;
        }
        if (newPassword !== verifyPassword) {
            showErrorPromptBox("两次密码不一致，请重新输入！", 2);
            return false;
        }

        let status = verifyUserOldPassword(oldPassword);
        if ("ERROR" === status) {
            showErrorPromptBox("旧密码错误，请重新输入", 3);
            $("#oldPassword").val("");
            return false;
        }

        let queryParams = {
            newPassword: newPassword
        }
        showWaitPromptBox("新密码信息保存中...", "");
        $.ajax({
            url: "updateUserPassword",
            type: 'post',
            data: queryParams,
            // async: false,
            beforeSend: (request) => {
                if (constant.csrfToken && constant.csrfHeader) {
                    request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                }
            },
            success: (data) => {
                if (data == "SUCCESS") {
                    closeWaitPromptBox();
                    showSuccessPromptBox("密码修改成功！", 2);
                } else if (data == "ERROR") {
                    closeWaitPromptBox();
                    showErrorPromptBox("密码存储失败！", 2);
                }
            },
            error: () => {
                closeWaitPromptBox();
                showErrorPromptBox("系统繁忙，请稍后访问！", 2);
            }
        });
        $("#oldPassword").val("");
        $("#newPassword").val("");
        $("#verifyPassword").val("");
    })

    let verifyUserOldPassword = (password) => {
        let verifyStatus = "ERROR";
        let queryParam = {
            password: password
        }
        $.ajax({
            url: "verifyUserOldPassword",
            type: 'post',
            data: queryParam,
            async: false,
            beforeSend: (request) => {
                if (constant.csrfToken && constant.csrfHeader) {
                    request.setRequestHeader(constant.csrfHeader, constant.csrfToken);
                }
            },
            success: (data) => {
                verifyStatus = data;
            },
            error: () => {
                showErrorPromptBox("系统繁忙，请稍后访问！", 2);
            }
        });
        return verifyStatus;
    }
</script>